package com.haojiang.mh.remote.ui.frag

import cn.com.heaton.blelibrary.ble.utils.ByteUtils
import com.haojiang.mh.R
import com.haojiang.mh.remote.base.BaseFrag
import com.haojiang.mh.remote.bean.TaskData
import com.haojiang.mh.remote.event.FunChangeEvent
import com.haojiang.mh.remote.utils.BleUtil
import com.haojiang.mh.remote.utils.BleUtil.Companion.isDevConncted
import com.haojiang.mh.remote.utils.BleUtil.Companion.lastConnect
import com.haojiang.mh.remote.utils.BleUtil.Companion.taskRun
import com.haojiang.mh.remote.utils.LogUtil
import com.haojiang.mh.remote.utils.SpUtil
import com.haojiang.mh.remote.widget.seekbar.SignSeekBar
import kotlinx.android.synthetic.main.frag_aroma.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class AromaFrag : BaseFrag() {

    private val SECTION_AROMA_MODE2_STARTUP = "section_aroma_mode2_startup"
    private val SECTION_AROMA_MODE3_STARTUP = "section_aroma_mode3_startup"
    private val SECTION_AROMA_MODE3_PAUSE = "section_aroma_mode3_pause"
    private var mode2StartupTime = 1 //默认开一分钟后关掉
    private var mode3StartupTime = 1 //默认开一分钟
    private var mode3PauseTime = 2 //一分钟后关掉 暂停2个小时


    private val onProgressChangedListener = object : SignSeekBar.OnProgressChangedListener {
        override fun onProgressChanged(signSeekBar: SignSeekBar?, progress: Int, progressFloat: Float, fromUser: Boolean) {
        }

        override fun getProgressOnActionUp(signSeekBar: SignSeekBar?, progress: Int, progressFloat: Float) {
            when (signSeekBar!!.id) {
                R.id.sbMode2StartupTime -> {
                    mode2StartupTime = progress
                    sendAromaData()
                }

                R.id.sbMode3StartupTime -> {
                    mode3StartupTime = progress
                    sendAromaData()
                }

                R.id.sbMode3PauseTime -> {
                    mode3PauseTime = progress
                    sendAromaData()
                }
            }
        }

        override fun getProgressOnFinally(signSeekBar: SignSeekBar?, progress: Int, progressFloat: Float, fromUser: Boolean) {
        }
    }

    private fun sendAromaData() {
        if (isDevConncted() && lastConnect.bleDeviceList.size > 0) {
            val taskList: ArrayList<TaskData> = arrayListOf(
                TaskData(Runnable {
                    val bytes = ByteUtils.intToBytes(mode2StartupTime)
                    lastConnect.bleDeviceList.forEach {
                        BleUtil.deviceDataSend(
                            it, BleUtil.getFinalBytes(
                                byteArrayOf(
                                    0x6E,
                                    0x1B,
                                    (bytes[0] + bytes[1]).toByte(),
                                    (bytes[2] + bytes[3]).toByte(),
                                    0x00
                                )
                            )
                        )
                    }
                }, 150),
                TaskData(Runnable {
                    val bytes = ByteUtils.intToBytes(mode3StartupTime)
                    lastConnect.bleDeviceList.forEach {
                        BleUtil.deviceDataSend(
                            it,
                            BleUtil.getFinalBytes(
                                byteArrayOf(
                                    0x6E,
                                    0x1C,
                                    (bytes[0] + bytes[1]).toByte(),
                                    (bytes[2] + bytes[3]).toByte(),
                                    0x00
                                )
                            )
                        )
                    }
                }, 150),
                TaskData(Runnable {
                    val bytes = ByteUtils.intToBytes(mode3PauseTime)
                    lastConnect.bleDeviceList.forEach {
                        BleUtil.deviceDataSend(
                            it,
                            BleUtil.getFinalBytes(
                                byteArrayOf(
                                    0x6E,
                                    0x1D,
                                    (bytes[0] + bytes[1]).toByte(),
                                    (bytes[2] + bytes[3]).toByte(),
                                    0x00
                                )
                            )
                        )
                    }
                }, 150),
            )
            taskRun(taskList)
//            val timeList = arrayListOf<Int>(
//                mode2StartupTime,
//                mode3StartupTime,
//                mode3PauseTime
//            )
            lastConnect.bleDeviceList.forEach {
                SpUtil.putData(context, SECTION_AROMA_MODE2_STARTUP, it.bleAddress, mode2StartupTime)
                SpUtil.putData(context, SECTION_AROMA_MODE3_STARTUP, it.bleAddress, mode3StartupTime)
                SpUtil.putData(context, SECTION_AROMA_MODE3_PAUSE, it.bleAddress, mode3PauseTime)
            }
        }
    }

    override fun onLayoutId(): Int {
        return R.layout.frag_aroma
    }

//    override fun initView() {
//        if(!EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().register(this)
//        }
//        return View.inflate(context, R.layout.frag_aroma, null)
//    }

    override fun initListener() {
        sbMode2StartupTime.getConfigBuilder()
            .min(1F) //最小值
            .max(60F) //最大值
            .progress(mode2StartupTime.toFloat())
            .sectionCount(6) //一共6段
//            .thumbColor(ContextCompat.getColor(this, R.color.sign_seek_bar_trace)) //
//            .sectionTextColor(ContextCompat.getColor(this, R.color.sign_seek_bar_text)) //背景色
            .sectionTextSize(17)
            .setUnit(getString(R.string.f_unit_min))
            .sectionTextPosition(SignSeekBar.TextPosition.BELOW_SECTION_MARK)
            .build()

        sbMode2StartupTime.setOnProgressChangedListener(onProgressChangedListener)

        sbMode3StartupTime.getConfigBuilder()
            .min(1F) //最小值
            .max(60F) //最大值
            .progress(mode3StartupTime.toFloat())
            .sectionCount(6) //一共6段
//            .thumbColor(ContextCompat.getColor(this, R.color.sign_seek_bar_trace)) //
//            .sectionTextColor(ContextCompat.getColor(this, R.color.sign_seek_bar_text)) //背景色
            .sectionTextSize(17)
            .setUnit(getString(R.string.f_unit_min))
            .sectionTextPosition(SignSeekBar.TextPosition.BELOW_SECTION_MARK)
            .build()

        sbMode3StartupTime.setOnProgressChangedListener(onProgressChangedListener)

        sbMode3PauseTime.getConfigBuilder()
            .min(1F) //最小值
            .max(12F) //最大值
            .progress(mode3PauseTime.toFloat())
            .sectionCount(11) //一共11段
//            .thumbColor(ContextCompat.getColor(this, R.color.sign_seek_bar_trace)) //
//            .sectionTextColor(ContextCompat.getColor(this, R.color.sign_seek_bar_text)) //背景色
            .sectionTextSize(17)
            .seekBySection()
            .setUnit(getString(R.string.f_unit_hour))
            .sectionTextPosition(SignSeekBar.TextPosition.BELOW_SECTION_MARK)
            .build()
        sbMode3PauseTime.setOnProgressChangedListener(onProgressChangedListener)
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onFunctionEvent(event: FunChangeEvent) {
        lastConnect.surface?.let {
//            if (it.hasAroma && !lastConnect.isAromaInit) {
            if (it.hasAroma) {
//                LogUtil.e("hasAroma-----")
//                lastConnect.isAromaInit = true
                if (lastConnect.bleDeviceList.size > 0) {
                    mode2StartupTime =
                        SpUtil.getData(
                            context,
                            SECTION_AROMA_MODE2_STARTUP,
                            lastConnect.bleDeviceList[0].bleAddress,
                            mode2StartupTime
                        ) as Int
                    mode3StartupTime =
                        SpUtil.getData(
                            context,
                            SECTION_AROMA_MODE3_STARTUP,
                            lastConnect.bleDeviceList[0].bleAddress,
                            mode3StartupTime
                        ) as Int
                    mode3PauseTime = SpUtil.getData(
                        context,
                        SECTION_AROMA_MODE3_PAUSE,
                        lastConnect.bleDeviceList[0].bleAddress,
                        mode3PauseTime
                    ) as Int
                    sbMode2StartupTime.setProgress(mode2StartupTime.toFloat())
                    sbMode3StartupTime.setProgress(mode3StartupTime.toFloat())
                    sbMode3PauseTime.setProgress(mode3PauseTime.toFloat())
                }
            }
        }
    }

    override fun initDestroy() {
        EventBus.getDefault().unregister(this)
    }
}